package arcane.jibx.io;

import java.io.File;
import java.util.ArrayList;

import arcane.network.UserAccount;
import arcane.object.game.map.LinkedMap;
import arcane.object.game.map.Tile;
import arcane.object.game.sprite.HumanoidActor;

public class Marshaller {
	public static String M1DIntAry(int[] ary) {
		StringBuilder b = new StringBuilder(ary.length * 3);
		int length = ary.length - 1;
		for (int i = 0; i < ary.length; i++) {
			if (i != length)
				b.append(ary[i] + ",");
			else
				b.append(ary[i]);
		}
		return b.toString();
	}

	public static int[] UM1DIntAry(String s) {
		StringBuilder b = new StringBuilder();
		String[] strRows = s.split(",");
		int[] ary = new int[strRows.length];
		for (int i = 0; i < strRows.length; i++) {
			ary[i] = Integer.parseInt(strRows[i]);
		}
		return ary;
	}

	public static String M2DIntAry(int[][] ary) {
		StringBuilder b = new StringBuilder(ary.length * 3);
		for (int i = 0; i < ary.length; i++) {
			b.append(M1DIntAry(ary[i])+"\n");
		}
		return b.toString();
	}

	public static int[][] UM2DIntAry(String s) {
		String[] strRows = s.split("\n");
		int[][] ary = new int[strRows.length][];
		for (int i = 0; i < strRows.length; i++) {
			ary[i] = UM1DIntAry(strRows[i]);
		}
		return ary;
	}

	public static String M2DBoolAry(boolean[][] ary) {
		StringBuilder b = new StringBuilder(ary.length * 3);
		int length;
		for (int i = 0; i < ary.length; i++) {
			length = ary[i].length - 1;
			for (int j = 0; j <= length; j++) {
				if (j != length)
					b.append(ary[i][j] ? "1," : "0,");
				else
					b.append(ary[i][j] ? "1\n" : "0\n");
			}
		}
		return b.toString();
	}

	public static Tile[][] UM2DTileAry(String s) {
		System.out.println("Unmarshalling 2D Tile Array: "+s);
		String[] strRows = s.split(" ");
		Tile[][] tiles = new Tile[strRows.length][];
		for (int i = 0; i < strRows.length; i++) {
			System.out.println("Row["+i+"]: "+strRows[i]);
			String[] cols = strRows[i].split("/");
			tiles[i] = new Tile[cols.length];
			for (int j = 0; j < cols.length; j++) {
				tiles[i][j] = new Tile(UM1DIntAry(cols[j]));
				//if(tiles[i][j].getLayers().length > 1)
				//System.out.println("Unmarshall: "+tiles[i][j]);
			}
		}
		return tiles;
	}

	public static String M2DTileAry(Tile[][] ary) {
		StringBuilder b = new StringBuilder();
		for (int i = 0; i < ary.length; i++) {
			for (int j = 0; j < ary[i].length; j++) {
				b.append(ary[i][j].toString() + "/");
			}
			b.append("\n");
		}
		return b.toString();
	}

	public static boolean[][] UM2DBoolAry(String s) {
		String[] strRows = s.split("\n");
		boolean[][] ary = new boolean[strRows.length][];
		for (int i = 0; i < strRows.length; i++) {
			String[] col = strRows[i].split(",");
			ary[i] = new boolean[col.length];
			for (int j = 0; j < col.length; j++) {
				ary[i][j] = col[j].equals("1");
			}
		}
		return ary;
	}

	public static void main(String[] args) {
		LinkedMap m = new LinkedMap(30, 20, "Images/old.png");
		HumanoidActor a = new HumanoidActor();
		UserAccount u = new UserAccount();
		JibxIO j = new JibxIO(LinkedMap.class);
		try {
			//j.save(new File("map1.xml"), m);
			m = (LinkedMap) j.load("map1.map", LinkedMap.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}